﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Policy;
using System.Web;
using System.Web.Mvc;
using Microsoft.Practices.Unity;
using Wicresoft.UnifyShow.Business;

namespace Wicresoft.UnifyShow2.Portal.Controllers
{
    [Authorize]
    public class AdminBSMaintainController : BaseController
    {
        private IUser_X user;
        private ISiteBranch siteBranch;
        private IBusinessSystem businessSystem;

        protected override void Dispose(bool disposing)
        {
            ((IDisposable)user).Dispose();
            ((IDisposable)siteBranch).Dispose();
            ((IDisposable)businessSystem).Dispose();
        }

        [InjectionConstructor]
        public AdminBSMaintainController(IUser_X user, ISiteBranch siteBranch, IBusinessSystem businessSystem)
        {
            this.user = user;
            this.siteBranch = siteBranch;
            this.businessSystem = businessSystem;
        }


        //
        // GET: /AdminBusinessSystem/

        public ActionResult Index()
        {
            base.SetRoles(user);
            ViewBag.IsConfigAdmin = IsConfigAdmin;
            var USER_X = user.GetUser_XByName(User.Identity.Name);
            if (USER_X == null)
                throw new Exception("USER_X is null.");
            ViewBag.UserDept = USER_X.Department;
            ViewBag.UserCompany = USER_X.Company;

            if (!IsConfigAdmin) return new HttpStatusCodeResult(403);

            var listModel = businessSystem.GetAllBusinessSystem();
            IList<SiteBranchModel> listSites = siteBranch.GetSiteALLBranch();
            Dictionary<decimal, string> idAndSiteNames = new Dictionary<decimal, string>();
            foreach (var item in listSites)
            {
                idAndSiteNames[item.ID] = item.Name;
            }
            ViewData["SiteIDAndName"] = idAndSiteNames;

            //Dictionary<decimal, string> idAndSiteNames = new Dictionary<decimal, string>();
            //List<BusinessSystemModel> listModel = new List<BusinessSystemModel>();
            //for (int i = 0; i < 10; i++)
            //{
            //    idAndSiteNames[i] = "SiteName" + i;
            //    listModel.Add(new BusinessSystemModel()
            //    {
            //        ID = i,
            //        Name = "BusinessSystem" + i,
            //        Category = "Category" + i,
            //        SiteID = i
            //    });
            //    listModel.Add(new BusinessSystemModel()
            //    {
            //        ID = i * 10 + 1,
            //        Name = "BusinessSystem" + i + 1,
            //        Category = "Category" + i + 1,
            //        SiteID = i
            //    });
            //    listModel.Add(new BusinessSystemModel()
            //    {
            //        ID = i * 100 + 1,
            //        Name = "BusinessSystem" + i + 1,
            //        Category = "Category" + i + 1,
            //        SiteID = i
            //    });


            //}
            //ViewData["SiteIDAndName"] = idAndSiteNames;
            return View(listModel);
        }

        public ActionResult Tree()
        {
            base.SetRoles(user);
            ViewBag.IsConfigAdmin = IsConfigAdmin;
            var USER_X = user.GetUser_XByName(User.Identity.Name);
            if (USER_X == null)
                throw new Exception("USER_X is null.");
            ViewBag.UserDept = USER_X.Department;
            ViewBag.UserCompany = USER_X.Company;

            if (!IsConfigAdmin) return new HttpStatusCodeResult(403);
             
            var listModel = new List<BusinessSystemModel>();
            IList<SiteBranchModel> listSites = new List<SiteBranchModel>();
            if (USER_X.Company == "福建省烟草专卖局")
            {
                listModel = businessSystem.GetAllBusinessSystem();
                listSites = siteBranch.GetSiteALLBranch();
            }
            else
            {
                SiteBranchModel site = siteBranch.GetSiteBranchByName(USER_X.Company);
                listSites.Add(site);
                var listModels = businessSystem.GetAllBusinessSystem();
                foreach (var model in listModels)
                {
                    if (model.SiteID == site.ID)
                    {
                        listModel.Add(model);
                    }
                }
            }
            Dictionary<decimal, string> idAndSiteNames = new Dictionary<decimal, string>();
            foreach (var item in listSites)
            {
                idAndSiteNames[item.ID] = item.Name;
            }
            ViewData["SiteIDAndName"] = idAndSiteNames;
            return PartialView(listModel);
        }
    }
}
